package cn.iocoder.yudao.module.train.controller.admin.studentcoursescore;

import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentCourseScoreStudentDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.studentcoursescore.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.studentcoursescore.StudentCourseScoreDO;
import cn.iocoder.yudao.module.train.service.studentcoursescore.StudentCourseScoreService;

@Tag(name = "管理后台 - 学生课程成绩")
@RestController
@RequestMapping("/train/student-course-score")
@Validated
public class StudentCourseScoreController {

    @Resource
    private StudentCourseScoreService studentCourseScoreService;

    @PostMapping("/create")
    @Operation(summary = "创建学生课程成绩")
    @PreAuthorize("@ss.hasPermission('train:student-course-score:create')")
    public CommonResult<Integer> createStudentCourseScore(@Valid @RequestBody StudentCourseScoreSaveReqVO createReqVO) {
        return success(studentCourseScoreService.createStudentCourseScore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学生课程成绩")
    @PreAuthorize("@ss.hasPermission('train:student-course-score:update')")
    public CommonResult<Boolean> updateStudentCourseScore(@Valid @RequestBody StudentCourseScoreSaveReqVO updateReqVO) {
        studentCourseScoreService.updateStudentCourseScore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学生课程成绩")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:student-course-score:delete')")
    public CommonResult<Boolean> deleteStudentCourseScore(@RequestParam("id") Integer id) {
        studentCourseScoreService.deleteStudentCourseScore(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除学生课程成绩")
                @PreAuthorize("@ss.hasPermission('train:student-course-score:delete')")
    public CommonResult<Boolean> deleteStudentCourseScoreList(@RequestParam("ids") List<Integer> ids) {
        studentCourseScoreService.deleteStudentCourseScoreListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学生课程成绩")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:student-course-score:query')")
    public CommonResult<StudentCourseScoreRespVO> getStudentCourseScore(@RequestParam("id") Integer id) {
        StudentCourseScoreStudentDTO studentCourseScore = studentCourseScoreService.getStudentCourseScore(id);
        return success(BeanUtils.toBean(studentCourseScore, StudentCourseScoreRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学生课程成绩分页")
    @PreAuthorize("@ss.hasPermission('train:student-course-score:query')")
    public CommonResult<PageResult<StudentCourseScoreRespVO>> getStudentCourseScorePage(@Valid StudentCourseScorePageReqVO pageReqVO) {
        PageResult<StudentCourseScoreStudentDTO> pageResult = studentCourseScoreService.getStudentCourseScorePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StudentCourseScoreRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学生课程成绩 Excel")
    @PreAuthorize("@ss.hasPermission('train:student-course-score:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStudentCourseScoreExcel(@Valid StudentCourseScorePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StudentCourseScoreStudentDTO> list = studentCourseScoreService.getStudentCourseScorePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "学生课程成绩.xls", "数据", StudentCourseScoreRespVO.class,
                        BeanUtils.toBean(list, StudentCourseScoreRespVO.class));
    }

}
